package com.myblog.web;

import com.myblog.Mapper.CommentMapper;
import com.myblog.po.Comment;
import com.myblog.po.User;
import com.myblog.service.BlogService;

import com.myblog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private BlogService blogService;


    private  String avatar = "/images/风铃公主.jpg";

    @GetMapping("/comments/{blogId}" )
public String comments(@PathVariable Long blogId, Model model)
{

    model.addAttribute("comments",commentService.listCommentByBlogId(blogId));
    model.addAttribute("blog",blogService.getBlog(blogId));
    return "blog::commentList";//
}

@PostMapping("/comments")
    public String post(Comment comment, HttpSession session){
     /*   Long blogid = comment.getBlog().getId();
*/
        comment.setBlog(blogService.getBlog(comment.getBlog_id()));



    User user = (User)session.getAttribute("user");
    if(user != null)
    {
        comment.setAvatar(user.getAvatar());
      //  comment.setNickname(user.getUsername());
        comment.setAdminComment(true);
    }else {
        comment.setAvatar("/image/1.jpg");
        comment.setAdminComment(false);
        //comment.setAvatar(avatar);
    }

    commentService.saveComment(comment);

        return "redirect:/comments/"+ comment.getBlog_id();
    }



}
